package com.wangrui.client.grid;

import java.util.LinkedHashMap;

import com.smartgwt.client.data.DSRequest;
import com.smartgwt.client.data.DataSource;
import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.types.Autofit;
import com.smartgwt.client.types.ExportDisplay;
import com.smartgwt.client.widgets.IButton;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.BooleanItem;
import com.smartgwt.client.widgets.form.fields.FormItem;
import com.smartgwt.client.widgets.form.fields.SelectItem;
import com.smartgwt.client.widgets.grid.ListGrid;
import com.smartgwt.client.widgets.grid.ListGridField;
import com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.client.widgets.layout.VLayout;
import com.smartgwt.client.types.ExportDisplay;   
import com.smartgwt.client.types.ExportFormat;   
import com.smartgwt.client.util.EnumUtil; 

public class Sipfa01GridTest extends VLayout{
	
	public Sipfa01GridTest(){
		
		DataSource worldDSExport = DataSource.get("sipfa01");  
		
		final ListGrid sipfa01TestList = new ListGrid();   
		sipfa01TestList.setWidth(500);   
		sipfa01TestList.setAlternateRecordStyles(true);   
		sipfa01TestList.setDataSource(worldDSExport);   
        sipfa01TestList.setAutoFetchData(true); 
        
        ListGridField OC_DATE = new ListGridField("OC_DATE", "OC_DATE");   
        ListGridField ZQGSDM = new ListGridField("ZQGSDM", "ZQGSDM");   
        ListGridField ZQGSMC = new ListGridField("ZQGSMC", "ZQGSMC");
        ListGridField JSCYRDM = new ListGridField("JSCYRDM", "JSCYRDM");   
        ListGridField JJXSRDM = new ListGridField("JJXSRDM", "JJXSRDM");   
        ListGridField JSCYRQSBH = new ListGridField("JSCYRQSBH", "JSCYRQSBH");
        ListGridField JSCYRJSZH = new ListGridField("JSCYRJSZH", "JSCYRJSZH");   
        ListGridField SJRQ = new ListGridField("SJRQ", "SJRQ");   
        
        
        sipfa01TestList.setFields(OC_DATE, ZQGSDM, ZQGSMC,JSCYRDM,JJXSRDM,JSCYRQSBH,JSCYRJSZH,SJRQ);   
        sipfa01TestList.setAutoFitData(Autofit.VERTICAL);   
        sipfa01TestList.setShowFilterEditor(true);   
        sipfa01TestList.setAutoFitMaxRecords(10); 
        
        final DynamicForm exportForm = new DynamicForm();   
        exportForm.setWidth(300);   
  
        SelectItem exportTypeItem = new SelectItem("exportType", "Export Type");   
        exportTypeItem.setWidth("*");   
        exportTypeItem.setDefaultToFirstOption(true);   
  
        LinkedHashMap valueMap = new LinkedHashMap();   
        valueMap.put("csv", "CSV (Excel)");   
        valueMap.put("xml", "XML");   
        valueMap.put("json", "JSON");   
        valueMap.put("xls", "XLS (Excel97)");   
        valueMap.put("ooxml", "XLSX (Excel2007/OOXML)");
        
        exportTypeItem.setValueMap(valueMap);   
        
        BooleanItem showInWindowItem = new BooleanItem();   
        showInWindowItem.setName("showInWindow");   
        showInWindowItem.setTitle("Show in Window");   
        showInWindowItem.setAlign(Alignment.LEFT);   
        
        IButton exportButton = new IButton("Export");   
        exportButton.addClickHandler(new ClickHandler() {   
            public void onClick(com.smartgwt.client.widgets.events.ClickEvent event) {   
                String exportAs = (String) exportForm.getField("exportType").getValue();   
  
                FormItem item = exportForm.getField("showInWindow");   
                boolean showInWindow =  item.getValue() == null ? false : (Boolean) item.getValue();   
  
                if(exportAs.equals("json")) {   
                    // JSON exports are server-side only, so use the OperationBinding on the DataSource   
                    DSRequest dsRequestProperties = new DSRequest();   
                    dsRequestProperties.setOperationId("customJSONExport");   
                    dsRequestProperties.setExportDisplay(showInWindow ? ExportDisplay.WINDOW : ExportDisplay.DOWNLOAD);   
  
                    sipfa01TestList.exportData(dsRequestProperties);   
                } else {   
                   // exportAs is either XML or CSV, which we can do with requestProperties   
                    DSRequest dsRequestProperties = new DSRequest();   
                    dsRequestProperties.setExportAs((ExportFormat)EnumUtil.getEnum(ExportFormat.values(), exportAs));   
                    dsRequestProperties.setExportDisplay(showInWindow ? ExportDisplay.WINDOW : ExportDisplay.DOWNLOAD);   
  
                    sipfa01TestList.exportData(dsRequestProperties);   
                }   
            }   
        });
  
        exportForm.setItems(exportTypeItem, showInWindowItem); 
        
        VLayout layout = new VLayout(15);   
        layout.setAutoHeight();   
  
        HLayout formLayout = new HLayout(15);   
        formLayout.addMember(exportForm);   
        formLayout.addMember(exportButton);   
        layout.addMember(formLayout);   
  
        layout.addMember(sipfa01TestList);
	}

}
